package com.example.login.Controller;

import com.example.login.Mapper.GoodsMap;
import com.example.login.Pojo.MobilePhone;
import com.example.login.Pojo.PCGoods;
import com.example.login.Pojo.mobileGoods;
import com.example.login.Pojo.mp3Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PayContoller {
    ArrayList<mobileGoods> mgList=new ArrayList<>(1);
    ArrayList<PCGoods> pcList=new ArrayList<>(1);
    ArrayList<mp3Goods> mp3List=new ArrayList<>(1);
    HashMap<String,Integer> map=new HashMap<>();
    @Autowired
    GoodsMap goodsMap;
    @RequestMapping("/user/pay")
    public String pay(Model model, HttpSession session)
    {
        mgList=(ArrayList<mobileGoods>) session.getAttribute("mobile");
        pcList= (ArrayList<PCGoods>)session.getAttribute("pc");
        mp3List =(ArrayList<mp3Goods>) session.getAttribute("mp3");
        map = (HashMap<String, Integer>)session.getAttribute("map");
        //添加销量
        for(int i=0;i<mgList.size();i++)
        {
            mobileGoods mg=mgList.get(i);
            int sell_num=map.get(mg.getId()+" "+mg.getSku_code());
            goodsMap.updateSellNum(sell_num,mg.getId());
        }
        for(int i=0;i<pcList.size();i++)
        {
            PCGoods pc=pcList.get(i);
            int sell_num=map.get(pc.getId()+" "+pc.getSku_code());
            goodsMap.updateSellNum(sell_num,pc.getId());
        }
        for(int i=0;i<mp3List.size();i++)
        {
            mp3Goods mp=mp3List.get(i);
            int sell_num=map.get(mp.getId()+" "+mp.getSku_code());
            goodsMap.updateSellNum(sell_num,mp.getId());
        }
        //改变订单状态

        return"/结算/购买成功";
    }
}
